#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 31 
typedef struct TNode{
    int Data;
    struct TNode *Left;
    struct TNode *Right;
}Tree;

Tree *PutBinTree(int s1[], int s2[], int k) {
    int i, t;
    if(k == 0) { 
        return NULL; 
    }
    Tree *BT = (Tree *)malloc(sizeof(Tree));
    t = s1[k-1];
    BT->Data = t;
    for(i = 0; i < k; i ++) {
        if(s2[i] == t) {  
            break;
        }
    }
    BT->Left = PutBinTree(s1, s2, i);
    BT->Right = PutBinTree(s1+i, s2+i+1, k-i-1);

    return BT;
}
void putBinTree(Tree *BT) {
    Tree *s[MAXSIZE];
    int t = 0, k = 1;
    s[t] = BT;
    while(t != k) {
        if(!t) {
            printf("%d", s[t]->Data); 
        }
        else {
            printf(" %d", s[t]->Data); 
        }
        if(s[t]->Left) {
            s[k] = s[t]->Left;
            k = (k+1) % MAXSIZE;
        }
        if(s[t]->Right) {
            s[k] = s[t]->Right;
            k = (k+1) % MAXSIZE;
        }
        t = (t+1) % MAXSIZE;
    }
}

int main()
{
    int i, n;
    int s1[MAXSIZE], s2[MAXSIZE];
    scanf("%d", &n);
    for(i = 0; i < n; i ++) {
        scanf("%d", &s1[i]);
    }
    for(i = 0; i < n; i ++) {
        scanf("%d", &s2[i]);
    }
    Tree *BT = PutBinTree(s1, s2, n);
    putBinTree(BT);

    return 0;
}
//树的遍历 